package vip.zcn.webmanagement.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vip.zcn.webmanagement.pojo.Dept;
import vip.zcn.webmanagement.service.DeptService;
import vip.zcn.webmanagement.utils.Result;

import java.util.List;

@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    // 查询所有部门
    @GetMapping()
    public Result list(){
        // 请求发送service层，从service中查询所有部门
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    // 删除部门
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id")
                             Integer id){
        // 测试
        // 调用service删除部门
        deptService.delete(id);
        return Result.success();
    }

    @PostMapping()
    public Result add(@RequestBody Dept dept){
        // 调用service添加部门
        deptService.add(dept);
        return Result.success();
    }
    // 根据ID查询部门
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id){
        // 请求发送service层，从service中查询部门
        Dept dept = deptService.get(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result edit(@RequestBody Dept dept){
        // 调用service修改部门
        deptService.edit(dept);
        return Result.success();
    }

}
